﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Carbon.Web.Forms.jQueryForms;

namespace Demo.Demos
{
    [Serializable]
    public class AnswerByCulture
    {
        #region Fields (1)
        private AnswerViewModel m_Parent = default(AnswerViewModel);
        #endregion

        #region Properties (5)
        public String Answer { get; set; }
        public int CultureId { get; set; }
        public String CultureName
        {
            get
            {
                return CultureInfo.GetCultureInfo(CultureId).EnglishName;
            }
        }
        public bool HasValue
        {
            get
            {
                return !string.IsNullOrEmpty(Answer);
            }
        }
        public AnswerViewModel Parent
        {
            get
            {
                return m_Parent;
            }
        }
        #endregion
    }

    [Serializable]
    public class AnswerViewModel
    {
        #region Fields (2)
        private PollViewModel m_Parent = default(PollViewModel);

        private List<AnswerByCulture> m_AnswersByCulture = new List<AnswerByCulture>();
        #endregion

        #region Properties (3)
        public IEnumerable<AnswerByCulture> AnswersByCulture
        {
            get
            {
                IDictionary<int, AnswerByCulture> answersByCulture = m_AnswersByCulture.ToDictionary(i => i.CultureId);

                ICollection<int> existingCultureIds = Parent.Cultures.Select(i => i.CultureId).ToArray();

                // remove values for cultures that are erased
                foreach (int cultureId in answersByCulture.Keys.ToArray())
                {
                    if (!existingCultureIds.Contains(cultureId))
                    {
                        answersByCulture.Remove(cultureId);
                    }
                }

                m_AnswersByCulture.Clear();

                foreach (PollCulture culture in Parent.Cultures)
                {
                    if (!answersByCulture.ContainsKey(culture.CultureId))
                    {
                        answersByCulture[culture.CultureId] = new AnswerByCulture() { CultureId = culture.CultureId };
                    }

                    m_AnswersByCulture.Add(answersByCulture[culture.CultureId]);
                }

                return m_AnswersByCulture;
            }
        }
        public DropDownListItemCollection<int, int> Order
        {
            get
            {
                List<int> numbers = new List<int>();
                for (int i = 0; i < Parent.Answers.Count; ++i)
                    numbers.Add(i);

                int? index = 0;
                for (int i = 0; i < Parent.Answers.Count; ++i)
                {
                    if (Parent.Answers[i].Equals(this))
                        index = i;
                }
                return new DropDownListItemCollection<int, int>(numbers, i => i, i => (i + 1).ToString(), index.Value, false);
            }
            set
            {
                DropDownListItemCollection<int, int> current = this.Order;

                if (current.SelectedKey != value.SelectedKey)
                {
                    int index = value.SelectedKey;

                    Parent.Answers.Remove(this);
                    Parent.Answers.Insert(index, this);
                }
            }
        }
        public PollViewModel Parent
        {
            get
            {
                return m_Parent;
            }
        }
        #endregion

        #region Constructors (1)
        public AnswerViewModel(PollViewModel parent)
        {
            if (parent == null) throw new ArgumentNullException("parent");
            this.m_Parent = parent;
        }
        #endregion

        #region Methods (1)

        public void Validate()
        {
            foreach (AnswerByCulture abc in AnswersByCulture)
            {
                if (string.IsNullOrEmpty(abc.Answer))
                    throw new ApplicationException("Answer for '" + abc.CultureName + "' not entered.");
            }
        }

        #endregion
    }
}